package com.sc.nft.controller;

import cn.hutool.core.util.StrUtil;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.controller.request.EcologyHatchConvertibilityCeilingRequest;
import com.sc.nft.controller.request.EcologyHatchIdRequest;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.vo.EcologyHatchExchangeConfVO;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalException;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.EcologyHatchExchangeConfService;
import com.sc.nft.service.UserEcologyHatchUpperLimitDetailsService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.concurrent.TimeUnit;

/**
 * @author 朱梦晨
 * @Description:
 * @date 2024-08-14
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/ecology/exchange")
@Api(tags = "生态熔炼")
public class EcologyHatchExchangeConfController {

    private final Redisson redisson;

    private final EcologyHatchExchangeConfService ecologyHatchExchangeConfService;

    private final UserEcologyHatchUpperLimitDetailsService userEcologyHatchUpperLimitDetailsService;

    @ApiOperation(value = "兑换生态熔炼上限配置列表", notes = "兑换生态铸造上限配置列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<EcologyHatchExchangeConfVO> exchangeList(@Valid @RequestBody EcologyHatchIdRequest request){
        return Result.success(ecologyHatchExchangeConfService.getExchangeCastUpperLimitConfList(UserPool.getUser(),request.getId()));
    }

    @ApiOperation(value = "兑换生态熔炼上限", notes = "兑换生态熔炼上限", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/exchange", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result exchange(@Valid @RequestBody EcologyHatchConvertibilityCeilingRequest req) throws GlobalException {
        UserInfo user = UserPool.getUser();
        if(!StrUtil.equals(req.getPayPwd(),user.getTransactionPassword())){
            return Result.fail(ErrorCode.PASSWORD_ERROR);
        }

        RLock lock = redisson.getLock(RedisKeyEnum.ECOLOGY_HATCH_CONVERTIBILITY_CAP_LOCK.getKey() + req.getEcologyHatchId());
        try {
            boolean res = lock.tryLock(10, 10, TimeUnit.SECONDS);
            if (res) {
                ecologyHatchExchangeConfService.ecologyHatchExchange(req.getEcologyHatchId(),req.getPropsId(),req.getCount(),user.getId());
            }else {
                return Result.fail(ErrorCode.LATER_TRY);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            throw new GlobalException("系统繁忙,请稍后重试!");
        } finally {
            lock.unlock();
        }
        return Result.success();
    }

}
